package com.ruoyi.web.controller.task;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.task.domain.TaskDetailResultParams;
import com.ruoyi.task.domain.TaskDetailExportResult;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.task.domain.TaskDetailResult;
import com.ruoyi.task.service.ITaskDetailResultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务分析结果明细Controller
 *
 * @author huangshibao
 * @date 2025-06-18
 */
@RestController
@RequestMapping("/task/risk")
public class TaskDetailResultController extends BaseController {
    @Autowired
    private ITaskDetailResultService taskDetailResultService;

    /**
     * 查询任务分析结果明细列表
     */
    @PreAuthorize("@ss.hasPermi('task:risk:list')")
    @RequestMapping("/list")
    public TableDataInfo list(TaskDetailResultParams taskDetailResult) {
        startPage();
        List<TaskDetailResult> list = taskDetailResultService.selectTaskDetailResultList(
                taskDetailResult);
        return getDataTable(list);
    }

    /**
     * 导出任务分析结果明细列表
     */
    @PreAuthorize("@ss.hasPermi('task:risk:export')")
    @Log(title = "任务分析结果明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskDetailResultParams taskDetailResult) {
        try {
            List<TaskDetailExportResult> list = taskDetailResultService.selectTaskDetailExportResult(
                    taskDetailResult);
            ExcelUtil<TaskDetailExportResult> util = new ExcelUtil<TaskDetailExportResult>(
                    TaskDetailExportResult.class);
            util.exportCsv(response, list, "risk");
        } catch (Exception e) {
            logger.error("task_risk_export error ", e);
            throw new RuntimeException(e);
        }
    }


    /**
     * 导出任务分析结果明细列表
     */
    @PreAuthorize("@ss.hasPermi('task:risk:export')")
    @Log(title = "任务分析结果明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemp")
    public AjaxResult exportTemp(TaskDetailResultParams taskDetailResult) {
        try {
            Map map = taskDetailResultService.exportTaskDetailResultList(taskDetailResult);
            return AjaxResult.success(map);
        } catch (Exception e) {
            logger.error("exportTemp error ", e);
            return AjaxResult.error("任务分析结果明细导出失败");
        }
    }

    /**
     * 获取任务分析结果明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:risk:query')")
    @GetMapping(value = "/{priKey}")
    public AjaxResult getInfo(@PathVariable("priKey") Long priKey) {
        return success(taskDetailResultService.selectTaskDetailResultByPriKey(priKey));
    }

    /**
     * 获取任务分析结果明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:risk:query')")
    @GetMapping(value = "images/{id}")
    public AjaxResult queryRiskImage(@PathVariable("id")  String id) {
        return success(taskDetailResultService.queryRiskImage(id));
    }

    /**
     * 新增任务分析结果明细
     */
    @PreAuthorize("@ss.hasPermi('task:risk:add')")
    @Log(title = "任务分析结果明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskDetailResult taskDetailResult) {
        taskDetailResult.setUpdateBy(getUsername());
        taskDetailResult.setCreateBy(getUsername());
        return toAjax(taskDetailResultService.insertTaskDetailResult(taskDetailResult));
    }

    /**
     * 修改任务分析结果明细
     */
    @PreAuthorize("@ss.hasPermi('task:risk:edit')")
    @Log(title = "任务分析结果明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskDetailResult taskDetailResult) {
        taskDetailResult.setUpdateBy(getUsername());
        return toAjax(taskDetailResultService.updateTaskDetailResult(taskDetailResult));
    }

    /**
     * 删除任务分析结果明细
     */
    @PreAuthorize("@ss.hasPermi('task:risk:remove')")
    @Log(title = "任务分析结果明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{priKeys}")
    public AjaxResult remove(@PathVariable Long[] priKeys) {
        return toAjax(taskDetailResultService.deleteTaskDetailResultByPriKeys(priKeys));
    }

}
